﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Application.Interfaces;
using Domain.Scheduling;
using Domain.Scheduling.Entities;

using Domain.Common;

namespace Application.Scheduling
{
   
    public class SchedulingManagementService : ISchedulingManagementService
    {
        public IScheduleStudyRepository ScheduleStudyRepository { get; private set; }
       
        public IUnitOfWork UnitOfWork { get; private set; }

        public SchedulingManagementService(IScheduleStudyRepository scheduleStudyRepository, IUnitOfWork unitOfWork)
        {
            if (scheduleStudyRepository == null)
            {
                throw new ArgumentException("A valid scheduleStudyRepository is required for this Domain Service.");
            }
            
            if (unitOfWork == null)
            {
                throw new ArgumentException("A valid Unit of work is required for this Domain Service");
            }

            this.ScheduleStudyRepository = scheduleStudyRepository;
            
            this.UnitOfWork = unitOfWork;
        }

        public List<ScheduleStudy> GetAllScheduleStudys()
        {
            return ScheduleStudyRepository.Get().ToList();
        }

        public ScheduleStudy AddScheduleStudy(ScheduleStudy scheduleStudy)
        {
            var item = ScheduleStudyRepository.Add(scheduleStudy);

            UnitOfWork.Save();

            return item;
        }

        public void UpdateScheduleStudy(ScheduleStudy scheduleStudy)
        {
            ScheduleStudyRepository.Update(scheduleStudy);

            UnitOfWork.Save();
        }

        public void DeleteScheduleStudy(ScheduleStudy scheduleStudy)
        {
            using (var transaction = new global::System.Transactions.TransactionScope())
            {


                ScheduleStudyRepository.Delete(scheduleStudy);

                UnitOfWork.Save();

                transaction.Complete();
            }
        }

       
    }
}
